/**
 * Copyright (C) 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gjc.client.dispatcher;

import java.util.logging.Logger;

import org.gjc.client.action.Action;
import org.gjc.client.callback.RequestCallbackRetry;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Default dispatcher.
 *
 * This is the place where batching and caching can be put when it's needed.
 *
 * @author <a href="mailto:mail@raphaelbauer.com">rEyez</<a>
 */
public class DefaultDispatchAsync implements DispatchAsync {

    Logger logger = Logger.getLogger(DefaultDispatchAsync.class.getName());

    private String fullServerWithPrefixWithSlashAtTheEnd;

    /**
     * @param fullServerWithPrefixWithSlashAtTheEnd eg "http://127.0.0.1:9666/api/v1/";
     */
    public DefaultDispatchAsync(String fullServerWithPrefixWithSlashAtTheEnd) {
        this.fullServerWithPrefixWithSlashAtTheEnd = fullServerWithPrefixWithSlashAtTheEnd;

        // Check against fatal error:
        if (!fullServerWithPrefixWithSlashAtTheEnd.endsWith("/")){
            assert false : "\"Host has to end with '/' => I will not work\"";
        }

    }

    public <A extends Action<R>, R> void execute(final A action,
            final AsyncCallback<R> asyncCallback) {

        RequestCallbackRetry<R> requestCallbackRetry
            = new RequestCallbackRetry<R>(fullServerWithPrefixWithSlashAtTheEnd, action, asyncCallback);

        requestCallbackRetry.runIt();

    }

}
